package com.im.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.im.entity.ImPrivateMessage;
import com.im.entity.vo.ImPrivateMessageVO;
import com.im.entity.vo.MessageStrategyVO;
import com.im.grpc.ChatMessageProto;
import com.im.grpc.chat.PrivateChatMessage;
import com.im.mapper.ImPrivateMessageMapper;
import com.im.service.IImPrivateMessageService;
import com.im.utils.BeanCopyUtils;
import com.im.utils.Redis.RedisSequenceGenerator;
import com.im.utils.page.ConditionDTO;
import com.im.utils.page.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author im
 * @since 2025-01-30
 */
@SuppressWarnings("SpringTransactionalMethodCallsInspection")
@Service
public class ImPrivateMessageServiceImpl extends ServiceImpl<ImPrivateMessageMapper, ImPrivateMessage> implements IImPrivateMessageService {

    @Autowired
    private RedisSequenceGenerator redisSequenceGenerator;
    @Override
    public List<ImPrivateMessage> pull(Long sessionId) {
        //根据会话id来查询消息(后续分页)
        return this.baseMapper.selectList(new LambdaQueryWrapper<ImPrivateMessage>().eq(ImPrivateMessage::getSessionId, sessionId).orderByAsc(ImPrivateMessage::getMessageId));
    }


    //消息保存
    public PrivateChatMessage saveMessageAndSessionUpdateAndSendMessage(PrivateChatMessage privateChatMessage){
        //生成消息id
        Long messageId = redisSequenceGenerator.getNextSequence(privateChatMessage.getSessionId());
        privateChatMessage.setMessageId(messageId);
        //消息保存
        saveMessage(privateChatMessage);
        return privateChatMessage;
    }
    //消息发送策略
    @Override
    public MessageStrategyVO messageStrategy(PrivateChatMessage bodyData) {
        //todo 消息发送策略
        //1.检测文本有没有敏感词
        //2.检测是否是好友
        MessageStrategyVO messageStrategyVO=new MessageStrategyVO();
        messageStrategyVO.setFlag(true);
        return messageStrategyVO;
    }

    //消息保存
    @Async("CommonThreadPoolExecutor")
    @Transactional
    public void saveMessage(PrivateChatMessage privateChatMessage){
        //构建消息实体
        ImPrivateMessage imPrivateMessage = BeanCopyUtils.copyObject(privateChatMessage, ImPrivateMessage.class);
        this.baseMapper.insert(imPrivateMessage);
        //todo 发送会话更新kafka 单纯更新会话即可

        //todo 发送消息推送kafka 进行消息推送需要组装消息

    }

    /**
     * 查询当前会话的分页数据
     * @param conditionDTO
     * @return
     */
    @Override
    public PageResult<ImPrivateMessageVO> pullPage(ConditionDTO conditionDTO) {
        Page<ImPrivateMessage> page = new Page<>(conditionDTO.getCurrent(), conditionDTO.getSize());
        // 查询日志列表
        Page<ImPrivateMessage> activityDTOPage = this.page(page, new LambdaQueryWrapper<ImPrivateMessage>()
                .eq(ImPrivateMessage::getSessionId, Long.parseLong(conditionDTO.getKeywords()))
                .orderByDesc(ImPrivateMessage::getTimestamp));
        List<ImPrivateMessageVO> imPrivateMessageVOS = BeanCopyUtils.copyList(activityDTOPage.getRecords(), ImPrivateMessageVO.class);
        return new PageResult<>(imPrivateMessageVOS,imPrivateMessageVOS.size());
    }
}
